﻿String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
$(function() {

    //--------------------建筑处理------------------
    $("#btnAddJZ1").bind("click", function() {

        var front = "";
        var last = "";
        var max = 0;
        var count = 0;
        //获取最大id
        $("tr[id^='jztr']").each(function() {
            front = $(this).attr("id");
            last = front.toString().substr(4, front.length - 4);
            if (Number(last) > max) {
                max = last;
            }
            count++;
        });
        $("a[id*='btnDelJZ']").show();
        var newid = Number(max) + 1;
        var lastNumber = count + 1;
        $("#tableJZ tr:nth-child(" + lastNumber + ")").after($("#jztr" + max).clone().attr("id", "jztr" + newid));
        $("#jztr" + newid + " input").each(function() {
            $(this).val(""); //清空数据
            $(this).css("background-color", "");
        })
        $("#jztr" + newid + " select").each(function() {
            $(this).get(0).selectedIndex = 0; //初始
            $(this).css("background-color", "");
        })
        $("#jztr" + newid + " a").each(function() {
            var count = 0;
            $(this).attr("id", "btnDelJZ" + newid);
            $(this).bind("click", function() {
                $("tr[id^='jztr']").each(function() {
                    count++;
                });
                if (count > 1) {
                    if (count == 2) {
                        $("a[id*='btnDelJZ']").each(function() {
                            $(this).hide();
                        })
                    }
                    $("#jztr" + newid).remove();
                }
            })
        })
    });
    $("a[id*='btnDelJZ']").each(function() {
        $(this).bind("click", function() {
            var count = 0;
            $("tr[id^='jztr']").each(function() {
                count++;
            });
            if (count > 1) {
                if (count == 2) {
                    $("a[id*='btnDelJZ']").each(function() {
                        $(this).hide();
                    })
                }
                $(this).parent().parent().remove();
            }
        });
    });

    $("input[type='checkbox'][id*='cbhh']").bind("click", function() {
        if ($(this).attr("checked") != "") {
            var subid = $(this).attr("id").toString().substr(22);
            var cbxx = "#ctl00_MainContent_cbxx" + subid;
            $(cbxx).attr("checked", "checked");
        }
    })

    $("input[type='checkbox'][id*='cbbb']").bind("click", function() {
        if ($(this).attr("checked") != "") {
            var subid = $(this).attr("id").toString().substr(22);
            var cbxx = "#ctl00_MainContent_cbxx" + subid;
            $(cbxx).attr("checked", "checked");
        }
    })
});

function GetEventSave() {
    var yanzheng = true;
    if (yanzheng == true) {
        yanzheng = YzRequired();
    }
    if (yanzheng == true) {
        yanzheng = jzdata(); //验建筑
    }
    if (yanzheng == true) {
        yanzheng = PDJL();
    }
    $("#ctl00_MainContent_HiddenField2").val("Save");
    return yanzheng;
}


function YzRequired() {
    var yanzheng = true;
    var message = "";
    if ($("#ctl00_MainContent_txtGcmc").val().Trim() == "") {
        message = message + "建设工程名称不能为空" + "\n";
        $("#ctl00_MainContent_txtGcmc").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtGcmc").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtGcdz").val().Trim() == "") {
        message = message + "建设工程名称不能为空" + "\n";
        $("#ctl00_MainContent_txtGcdz").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtGcdz").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtJcrq").val().Trim() == "") {
        message = message + "检查日期不能为空" + "\n";
        $("#ctl00_MainContent_txtJcrq").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtJcrq").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtJsdwmc").val().Trim() == "") {
        message = message + "建设单位名称不能为空" + "\n";
        $("#ctl00_MainContent_txtJsdwmc").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtJsdwmc").css("background-color", "");
    }
    if ($("#ctl00_MainContent_txtPdjl").val().Trim() == "") {
        message = message + "竣工验收消防检查基本情况和综合评定结论不能为空" + "\n";
        //$("#ctl00_MainContent_txtPdjl").css("background-color", "plum");
    }
    else {
        if ($("#ctl00_MainContent_txtPdjl").val().Trim().length > 330) {
            message = message + "竣工验收消防检查基本情况和综合评定结论超过限定字数" + "\n";
        }
        else {
            $("#ctl00_MainContent_txtPdjl").css("background-color", "");
        }
    }
    if ($("#ctl00_MainContent_txtJcry").val().Trim() == "") {
        message = message + "竣工验收消防检查人员不能为空" + "\n";
        $("#ctl00_MainContent_txtJcry").css("background-color", "plum");
    }
    else {
        $("#ctl00_MainContent_txtJcry").css("background-color", "");
    }
    if (message != "") {
        yanzheng = false;
        alert(message);
    }
    return yanzheng;
}
//验评定结论
function PDJL() {
    var yanzheng = true;
    $("input[id*='cbxx'][type='checkbox']").each(function() {
        var subid = $(this).attr("id").toString().substr(22);
        var hhid = "#ctl00_MainContent_cbhh" + subid;
        var bbid = "#ctl00_MainContent_cbbb" + subid;
        if ($(this).attr("checked") != "") {
            if ($(hhid).attr("checked") == "" && $(bbid).attr("checked") == "") {
                $(hhid).parent().css("background-color", "plum");
                yanzheng = false;
            }
            else {
                $(hhid).parent().css("background-color", "");
            }
        }
    });
    if (yanzheng == false) {
        alert("请选择评定结论");
    }
    return yanzheng;
}

//建筑验证
function jzdata() {

    var jzdata = "";
    var yanzheng = true;
    var xiaoshu = ['txtJzgd', 'txtJzmj'];
    $("tr[id^='jztr']").each(function() {
        if ($(this).css("display") != "none") {
            $(this).children("td").each(function() {
                $(this).children("input").each(function() {
                    var id = $(this).attr("id").toString().Trim();
                    var subid = id.substr(id.search("txt"), id.length);
                    var bl = true;
                    switch (subid) {
                        case "txtGclbmc":
                        case "txtJzmj":
                        case "txtCs":
                        case "txtJzgd":
                            if ($(this).val().toString().Trim() == "") {
                                $(this).css("background-color", "plum");
                                yanzheng = false;
                                bl = false;
                            }
                            else {
                                jzdata += subid + "★" + $(this).val() + "☆";
                                $(this).css("background-color", "");
                            }
                            break;
                    }
                    if (bl == true) {
                        switch (subid) {
                            case "txtJzgd":
                                var mode = /^\d{1,8}(\.\d{1,2})?$/;
                                if (!mode.test($(this).val())) {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            case "txtZdmj":
                                if ($(this).val().Trim() != "") {
                                    var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                    if (!mode.test($(this).val())) {
                                        $(this).css("background-color", "plum");
                                        yanzheng = false;
                                    }
                                    else {
                                        jzdata += subid + "★" + $(this).val() + "☆";
                                        $(this).css("background-color", "");
                                    }
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            case "txtJzmj":
                                var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                if (!mode.test($(this).val())) {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            default:
                                jzdata += subid + "★" + $(this).val() + "☆";
                                break;
                        }
                    }
                })
                $(this).children("select").each(function() {
                    var id = $(this).attr("id").toString().Trim();
                    var subid = id.substr(id.search("drop"), id.length);
                    $(this).children("option").each(function() {
                        if ($(this).attr("selected") != "") {
                            if ($(this).attr("value") != "#") {
                                jzdata += subid + "★" + $(this).attr("value") + "◇" + $(this).text() + "☆";
                            }
                            else {
                                jzdata += subid + "★◇☆";
                            }
                        }
                    })
                });
            });
        }
    });
    if (jzdata.length > 0) {
        jzdata = jzdata.substr(0, jzdata.length - 1);
    }
    if (yanzheng == false) {
        alert("建筑数据格式不正确");
    }
    $("#ctl00_MainContent_HiddenField1").val(jzdata);
    return yanzheng;
}